<?php
//include_once ("/home/saludaun/public_html/expediente/library/classes/postmaster.php");

function validEmail($email){
    if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
    return true;
    }
    return false;
}

function emailLogin($patient_id, $message, $connection){
	$query = "SELECT * FROM `patient_data` WHERE `pid`= $patient_id";
	$res = mysqli_query($connection, $query);
	$patientData = mysqli_fetch_array($res);
    
    if ( $patientData['hipaa_allowemail'] != "YES" || empty($patientData['email'])   ) {
        return false;
    }
    if (!(validEmail($patientData['email']))) {
        return false;
    }
	   
	$mail = new PHPMailer(); // defaults to using php "mail()"
	$mail->AddReplyTo("info@saludaunclic.com", "info@saludaunclic.com");
        $mail->SetFrom("info@saludaunclic.com","info@saludaunclic.com");
        $mail->AddAddress($patientData);
        $mail->Subject = "Recordatorio de citas automatico.";
        $mail->MsgHTML("<html><body><div class='wrapper'>".$message."</div></body></html>");
        $mail->IsHTML(true);
	//$mail->Username = "info@saludaunclic.com";
	//$mail->Password = "";	
	//$mail->From = "info@saludaunclic.com";
	//$mail->FromName = "Salud a un clic";
	//$mail->AddAddres = $patientData['email'];	
	//$mail->WordWrap = 50;
	//$mail->Subject    = "Recordatorio de Citas, Salud a un Clic.";	
	//$mail->Body = $message;	
	//$body = $message;	
	//$mail->SetFrom('info@saludaunclic.com', 'Equipo de Salud a un Clic');
	//$mail->AddReplyTo('info@saludaunclic.com',"Equipo de Salud a un Clic");
	//$address = $patientData['email'];
	//$mail->AddAddress($address, $patientData['fname'] . " " .$patientData['lname']);
	//$mail->Subject    = "PHPMailer Test Subject via mail(), basic";
	//$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
	//$mail->MsgHTML($body);
	//$mail->AddAttachment("images/phpmailer.gif");      // attachment
	//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
	if(!$mail->Send()) {
		echo "Mailer Error: " . $mail->ErrorInfo;
	}else{
		echo "Message sent!";
	}
}

function sendmail($pid, $aid, $startTime, $mfname, $mlname, $pfname, $plname, $connection){
    $message= ' '
    .'<p>'
		.' Estimado' . $pfname . ' ' . $plname . ': '
    .'</p>'
    .'<p>'
		. 'Le recordamos que usted tiene una cita el dia de ma�ana con el(la) dr(a). ' . $mfname . ' ' . $mlname . '<br>'
		. 'La cita esta programada para la siguiente hora: ' . $startTime . ' <br><br> '
		. 'Saludos cordiales.'
    . '</p>';
    
    emailLogin($pid, $message, $connection);
}



//  SAC
//  MySQL Config
/*
$host	= 'localhost';
$port	= '3306';
$login	= 'root';
$pass	= '';
$dbase	= 'sacexp';

//Added ability to disable
//utf8 encoding - bm 05-2009
global $disable_utf8_flag;
$disable_utf8_flag = false;

$sqlconf = array();
global $sqlconf;
$sqlconf["host"]= $host;
$sqlconf["port"] = $port;
$sqlconf["login"] = $login;
$sqlconf["pass"] = $pass;
$sqlconf["dbase"] = $dbase;
//////////////////////////
//////////////////////////
//////////////////////////
//////DO NOT TOUCH THIS///
$config = 1; /////////////
//////////////////////////
//////////////////////////
//////////////////////////


global $host,$port,$login,$pass,$dbase,$disable_utf8_flag;
if (!$disable_utf8_flag) {
	$pnconfig['utf8Flag'] = true;
}else{
	$pnconfig['utf8Flag'] = false;
}


$connection =  mysqli_connect($sqlconf["host"].":".$sqlconf["port"], $sqlconf["login"], $sqlconf["pass"], $sqlconf["dbase"]);
if (mysqli_connect_errno()) {
	echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
echo '<br>Conectado satisfactoriamente<br>';
*/

$anyday = date("Y-m-j", strtotime("+1 days"));
$query = ''
	.' SELECT pce.pc_pid pid, pce.pc_aid aid, pce.pc_startTime startTime, u.fname mfname, u.lname mlname, pd.fname pfname, pd.lname plname '
	.' FROM openemr_postcalendar_events pce inner join users u on (pce.pc_aid = u.id) '
	.' inner join patient_data pd on (pce.pc_pid = pd.pid)'
	.' WHERE pc_eventDate = "' . $anyday. '"' ;
$result = mysqli_query($connection, $query);

die("MTFD");

while($row = mysqli_fetch_array($result)) {
	//echo $row['pid'] . " " . $row['aid'] . " " . $row['startTime'] . " " . $row['mfname'] . " " . $row['mlname'] . " " . $row['pfname']. " " . $row['plname'];
	echo "<br>";
	echo "i";
	sendmail($row['pid'], $row['aid'], $row['startTime'], $row['mfname'], $row['mlname'], $row['pfname'], $row['plname'], $connection);
}
/*mysqli_close($connection);
echo "<br>FIN<br>";*/
?>
